package com.terren.webdig.common.util;

import java.util.ArrayList;
import java.util.List;

public class ColorUtil {

	/**
	 * 将 rgb 数值转换成颜色
	 * 
	 * @param red
	 * @param green
	 * @param blue
	 * @return
	 */
	public static String toRGBString(int red, int green, int blue) {
		String hashRed = Integer.toHexString(red);
		String hashGreen = Integer.toHexString(green);
		String hashBlue = Integer.toHexString(blue);

		if (hashRed.length() < 2) {
			hashRed = "0" + hashRed;
		}
		if (hashGreen.length() < 2) {
			hashGreen = "0" + hashGreen;
		}
		if (hashBlue.length() < 2) {
			hashBlue = "0" + hashBlue;
		}

		String rgbColor = "#" + hashRed + hashGreen + hashBlue;
		return rgbColor;
	}

	/** 随机颜色RGB **/
	public static List<String> getSortColor(int colorCount) {
		List<String> colorArray = new ArrayList<String>();

		int redNumber = 255;
		int greenNumber = 0;
		int blueNumber = 0;

		// 红
		String oneColor = toRGBString(redNumber, greenNumber, blueNumber);
		colorArray.add(oneColor);

		int totalNumber = 1275; // 255 * 5;

		int needColor = colorCount - 1;
		// 余数
		int lastNumber = totalNumber % needColor;

		// 每个颜色之间的差值 取整
		int shang = totalNumber / needColor;

		// 在剩余几个颜色中 将余数差值加进去
		int lastIndex = needColor - lastNumber;

		/** 用来控制 rgb 加减 **/
		int offset = shang;

		int colorIndex = 0;
		while (colorIndex < needColor) {// 红绿，红 255，绿0，绿加，直到255
			if (shang > 0) {
				greenNumber = greenNumber + offset;
				// 把 余数 加进去
				if (colorIndex >= lastIndex) {
					lastNumber--;
					greenNumber = greenNumber + 1;
				}

				if (greenNumber > 255) {
					offset = greenNumber - 255;
					greenNumber = 255;

					if (colorIndex >= lastIndex) {
						offset--;// 在下次执行时加上
					}

					break;
				} else {
					oneColor = toRGBString(redNumber, greenNumber, blueNumber);
					colorArray.add(oneColor);

					offset = shang;
					colorIndex++;
				}
			} else {
				// 总数量超过 1275 暂时不考虑那么多
			}
		}

		while (colorIndex < needColor) {// 绿，绿 255，红255，红减，直到为0
			if (shang > 0) {
				redNumber = redNumber - offset;
				// 把 余数 加进去
				if (colorIndex >= lastIndex) {
					lastNumber--;
					redNumber = redNumber - 1;
				}

				if (redNumber < 0) {
					offset = 0 - redNumber;
					redNumber = 0;

					if (colorIndex >= lastIndex) {
						offset--;// 在下次执行时加上
					}

					break;
				} else {
					oneColor = toRGBString(redNumber, greenNumber, blueNumber);
					colorArray.add(oneColor);

					offset = shang;
					colorIndex++;
				}
			} else {
				// 总数量超过 1275 暂时不考虑那么多
			}
		}

		while (colorIndex < needColor) {// 绿蓝，绿 255，蓝0，蓝加，直到 255
			if (shang > 0) {
				blueNumber = blueNumber + offset;
				// 把 余数 加进去
				if (colorIndex >= lastIndex) {
					lastNumber--;
					blueNumber = blueNumber + 1;
				}

				if (blueNumber > 255) {
					offset = blueNumber - 255;
					blueNumber = 255;

					if (colorIndex >= lastIndex) {
						offset--;// 在下次执行时加上
					}

					break;
				} else {
					oneColor = toRGBString(redNumber, greenNumber, blueNumber);
					colorArray.add(oneColor);

					offset = shang;
					colorIndex++;
				}
			} else {
				// 总数量超过 1275 暂时不考虑那么多
			}
		}

		while (colorIndex < needColor) {// 蓝，绿 255，蓝255，绿减，直到为0
			if (shang > 0) {
				greenNumber = greenNumber - offset;
				// 把 余数 加进去
				if (colorIndex >= lastIndex) {
					lastNumber--;
					greenNumber = greenNumber - 1;
				}

				if (greenNumber < 0) {
					offset = 0 - greenNumber;
					greenNumber = 0;

					if (colorIndex >= lastIndex) {
						offset--;// 在下次执行时加上
					}

					break;
				} else {
					oneColor = toRGBString(redNumber, greenNumber, blueNumber);
					colorArray.add(oneColor);

					offset = shang;
					colorIndex++;
				}
			} else {
				// 总数量超过 1275 暂时不考虑那么多
			}
		}

		while (colorIndex < needColor) {// 红蓝，蓝 255，红0，红加，直到255
			if (shang > 0) {
				redNumber = redNumber + offset;
				// 把 余数 加进去
				if (colorIndex >= lastIndex) {
					lastNumber--;
					redNumber = redNumber + 1;
				}

				if (redNumber > 255) {
					offset = redNumber - 255;
					redNumber = 255;

					if (colorIndex >= lastIndex) {
						offset--;// 在下次执行时加上
					}

					break;
				} else {
					oneColor = toRGBString(redNumber, greenNumber, blueNumber);
					colorArray.add(oneColor);

					offset = shang;
					colorIndex++;
				}
			} else {
				// 总数量超过 1275 暂时不考虑那么多
			}
		}

		return colorArray;
	}

	/**
	 * 获得跟背景颜色对比鲜明的颜色
	 * 
	 * @param bgColor
	 * @return
	 */
	public String getFontColor(String bgColor) {
		String fontColor = "";

		int minIndex = 0;
		int middleNumber = 255 / 2;

		String red = bgColor.substring(1, 3);
		String green = bgColor.substring(3, 5);
		String blue = bgColor.substring(5);

		int redNumber = Integer.parseInt(red, 16);
		int greenNumber = Integer.parseInt(green, 16);
		int blueNumber = Integer.parseInt(blue, 16);

		System.out.println(redNumber + " | " + greenNumber + " | " + blueNumber);

		int miniNumber = redNumber;

		if (greenNumber < miniNumber) {
			miniNumber = greenNumber;
			minIndex = 1;
		}
		if (blueNumber < miniNumber) {
			miniNumber = blueNumber;
			minIndex = 2;
		}

		if (miniNumber > middleNumber) {// 黑色
			fontColor = "#000000";
		} else {
			if (minIndex == 0) {// 红色
				System.out.println("111");
				fontColor = "#FF0000";
			} else if (minIndex == 1) {// 绿色
				System.out.println("222");
				fontColor = "#00FF00";
			} else {// 蓝色
				System.out.println("333");
				fontColor = "#0000FF";
			}
		}
		return fontColor;
	}

	/** 自定义的排序颜色 **/
	public static List<String> getSortColor2(int total) {
		List<String> sortList = new ArrayList<String>();
		String[] colorArray = { "#E51400", "#FF00A8", "#A200FF", "#D80073", "#E671B8", "#FA6801", "#F09609", "#FFC600",
				"#D8C101", "#A2C139", "#339933", "#008A00", "#6D8764", "#00ABA9", "#00B4D2", "#1BA1E2", "#0064FF",
				"#0050EF", "#6A00FF", "#6A64FF", "#A20025", "#A05000", "#7B3A3E", "#765F89" };

		int colorLength = colorArray.length;

		if (total <= colorLength) {// 当总数小于颜色数量时
			sortList.add(colorArray[0]);

			int need = total - 1;
			int lastLength = colorLength - 1;

			// 余数
			int yu = lastLength % need;

			// 间隔数
			int shang = lastLength / need;

			int offsetIndex = need - yu;

			int colorIndex = 0;
			for (int i = 0; i < need; i++) {
				colorIndex = colorIndex + shang;

				if (i >= offsetIndex) {
					colorIndex = colorIndex + 1;
				}
				sortList.add(colorArray[colorIndex]);
			}
		} else {// 当颜色数量不够时，重复
				// 余数
			int yu = total % colorLength;

			//
			int shang = total / colorLength;

			int offsetIndex = colorLength - yu;

			int colorIndex = 0;
			int repeat = shang;

			while (total > 0) {
				if (colorIndex >= offsetIndex) {
					repeat++;
				}

				for (int i = repeat; i > 0; i--) {
					sortList.add(colorArray[colorIndex]);
					total--;
				}
				colorIndex++;
				repeat = shang;
			}
		}
		return sortList;
	}

	public static void main(String[] args) {
		System.out.println("start ... ");

		List<String> sortList = getSortColor2(27);

		int i = 1;
		for (String str : sortList) {
			System.out.println(i + " : " + str);
			i++;
		}

		System.out.println("finished ... ");
	}

}
